/*
* @(#)ListDmsSelectSingleAction.java
*
* Copyright (c) 2003 DCIVision Ltd
* All rights reserved.
*
* This software is the confidential and proprietary information of DCIVision
* Ltd ("Confidential Information").  You shall not disclose such Confidential
* Information and shall use it only in accordance with the terms of the license
* agreement you entered into with DCIVision Ltd.
*/
package com.dcivision.dms.web;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionMapping;

import com.dcivision.dms.DmsOperationConstant;
import com.dcivision.dms.bean.DmsDocument;
import com.dcivision.dms.bean.DmsRoot;
import com.dcivision.dms.core.DocumentRetrievalManager;
import com.dcivision.dms.core.DocumentValidateManager;
import com.dcivision.dms.core.RootOperationManager;
import com.dcivision.dms.core.RootRetrievalManager;
import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.ErrorConstant;
import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.SystemFunctionConstant;
import com.dcivision.framework.TextUtility;
import com.dcivision.framework.web.AbstractListAction;
import com.dcivision.framework.web.AbstractSearchForm;

/**
 * <p>Class Name:       ListDmsSelectSingleAction.java    </p>
 * <p>Description:      The list action class for ListDmsSelectSingle.jsp</p>
 *
 *    @author           Angus Shiu
 *    @company          DCIVision Limited
 *    @creation date    18/09/2003
 *    @version          $Revision: 1.14 $
 */

public class ListDmsSelectSingleAction extends AbstractListAction {

  public static final String REVISION = "$Revision: 1.14 $";


  /**
   *  Constructor - Creates a new instance of ListArchiveAction and define the default listName.
   */
  public ListDmsSelectSingleAction() {
    super();
    this.setListName("dmsSelectSingleList");
    this.setListFunctionName("getDocumentList");
  }

  /**
   * getMajorDAOClassName
   *
   * @return  The class name of the major DAObject will be used in this action.
   */
  public String getMajorDAOClassName() {
    return("com.dcivision.dms.dao.DmsDocumentDAObject");
  }

  /**
   * getFunctionCode
   *
   * @return  The corresponding system function code of action.
   */
  public String getFunctionCode() {
    return(SystemFunctionConstant.DMS_PUBLIC_FOLDER);
  }

  public void getListData(ActionMapping mapping, AbstractSearchForm form, HttpServletRequest request, HttpServletResponse response) throws ApplicationException {
    SessionContainer sessionContainer = this.getSessionContainer(request);
    Connection conn = this.getConnection(request);
    ListDmsSelectSingleForm docSearchForm = (ListDmsSelectSingleForm) form;
    DocumentRetrievalManager docRetrievalManager = new DocumentRetrievalManager(sessionContainer, conn);
    RootOperationManager rootOperationManager = new RootOperationManager(sessionContainer, conn);
    RootRetrievalManager rootRetrievalManager = new RootRetrievalManager(sessionContainer, conn);
    String myPageFlag = request.getParameter("pageFlag");
    String onlyPublic = request.getParameter("onlyPublic");

    try {
      //Retrieve the accessible public root list.
      List publicRootList = null;
      if ("true".equals(onlyPublic) || "TRUE".equals(onlyPublic)) {
        publicRootList = rootRetrievalManager.getAccessiblePublicRootsList();
      }else {
        publicRootList = rootRetrievalManager.getAllAccessibleRootsList();
      }
      request.setAttribute("publicRootList", publicRootList);
      
      //Retrieve dms_root root and dms_document root.
      DmsRoot dmsRoot = docRetrievalManager.getDmsRoot(TextUtility.parseIntegerObj(docSearchForm.getRootID()));
      DmsDocument documentRoot = docRetrievalManager.getRootFolderByRootID(TextUtility.parseIntegerObj(docSearchForm.getRootID()));
      String locationPath = docRetrievalManager.getLocationPath(new Integer(docSearchForm.getParentID()));
      if (("true".equals(onlyPublic) || "TRUE".equals(onlyPublic)) && DmsRoot.PERSONAL_ROOT.equals(dmsRoot.getRootType())) {
        dmsRoot = (DmsRoot)publicRootList.get(0);
        documentRoot = docRetrievalManager.getRootFolderByRootID(dmsRoot.getID());
        locationPath = docRetrievalManager.getLocationPath(documentRoot.getID());
        docSearchForm.setRootID(dmsRoot.getID().toString());
        docSearchForm.setParentID(documentRoot.getID().toString());
        docSearchForm.setDestinationPath(locationPath);
      }
      request.setAttribute("dmsRoot", dmsRoot);
      request.setAttribute("rootFolder", documentRoot);
      request.setAttribute("location", locationPath);

      // Call the super list function.
      DocumentValidateManager docValidateManager = new DocumentValidateManager(sessionContainer, conn);
      // check permission
      boolean havePermission = docValidateManager.checkHasPermissionOnObject(GlobalConstant.OBJECT_TYPE_DOCUMENT, TextUtility.parseIntegerObj(docSearchForm.getParentID()), TextUtility.parseIntegerObj(docSearchForm.getRootID()), "R");
      if (havePermission) {
      	if(DmsOperationConstant.USE_DOC_FOLDER_PICKER.equals(myPageFlag)){
      		this.setListFunctionName("getDocumentListDocFolder");
      		//this.setListFunctionName("getDocumentList");
      	}else{
      		this.setListFunctionName("getDocumentList");
      	}
      	
        super.getListData(mapping, docSearchForm, request, response);

        List originalList = (ArrayList)request.getAttribute(this.m_sListName);
        List resultList = new ArrayList();

        for (int i = 0; i < originalList.size(); i++) {
          DmsDocument dmsDocument = (DmsDocument)originalList.get(i);
          List tmpList = docRetrievalManager.getVersionListByDocumentID(dmsDocument.getID());
          //dmsDocument.setVersionList(tmpList);
          resultList.add(tmpList);
        }
        request.setAttribute(this.m_sListName + "_versionList", resultList);
      }else{
        request.setAttribute(this.m_sListName + "_versionList", new ArrayList());
      }

      //this.commit(request);
      sessionContainer=null;
      docRetrievalManager.release();
      rootOperationManager.release();
      rootRetrievalManager.release();
      docValidateManager.release();

    } catch (ApplicationException appEx) {
      this.rollback(request);
      throw appEx;
    } catch (Exception e) {
      log.error(e, e);
      this.rollback(request);
      throw new ApplicationException(ErrorConstant.COMMON_FATAL_ERROR, e);
    }finally{
      conn=null;
    }
  }
}
